package com.raychi.demo.servlet.custom;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.raychi.demo.entity.Custom;
import com.raychi.demo.util.CustomUtil;
import com.raychi.demo.util.JdbcUtil;

public class CustomSevlet extends HttpServlet{


	private static final long serialVersionUID = 1;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			List<Custom> list = getcustoms();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/index1.jsp").forward(request, response);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}catch (SQLException e){
			e.printStackTrace();
		}
		
			
	}
	
	private List<Custom> getcustoms() throws ClassNotFoundException, SQLException{
		List<Custom> customs = new ArrayList<Custom>();
		Connection conn = JdbcUtil.getConnection();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("select * from custom");
		Custom custom = null;
		while(rs.next()){
			custom = CustomUtil.createCustom(rs);
			customs.add(custom);
		}
		return customs;
	}
	
	
	
}
